# coding:utf8

'''
    java 文件转 py  

    java :
      包 : package com.dtr.zxing.camera;
      导入外部库 : import java.util.ArrayList;
      公共类 : public class AutoFocusManager implements Camera.AutoFocusCallback {}
      非公共类 : 
      
    类 : 
        类名
        父类, 接口
        成员变量字典
        成员函数字典
    
    变量 :
        变量名
        变量类型
        
    函数 :
        函数名
        函数局部变量
        表达式\语句 块
        
    转换规则 :
        不带初始化的变量
        带初始化的变量
        库函数字典
        类字典
        语句字典
        注释字典
'''

infile = file('CmProductInfoAction.java', 'r')

import re

class JAVar:
    varname = ''
    vartype = None
    
class JABlcok:
    pass
    
class JAFun:
    funname = ''
    funvars = []
    funblock = []

class JAClass:
    classname = ''
    parents = []
    interfaces = []
    vars = {}
    funs = {}

class JAP:
    """
    文件的注释
    文件的包名
    引用的外部库
    文件的类
    文件的私有类
    """
    comments = []
    package = ''
    outlib = []
    pubclass = JAClass()
    privateclass = []


def getlinetype(line):
    packagepattern = re.compile(r'^package .*;')
    # s = 'package com.dtr.zxing.camera;'

    match = packagepattern.match(line)
    if match:
        return 'package'
        
    libpattern = re.compile(r'^import .*;')
    match = libpattern.match(line)
    if match:
        return 'publib'
        
    linecommentpattern = re.compile(r'[\t ]*//.*|[\t ]*/\*[^(\*/)]*\*/ *')  # 匹配单行注释
    match = linecommentpattern.match(line)
    if match:
        return 'linecomment'
        
    startblockcommentpattern = re.compile(r'[\t ]*/\*[^(\*/)]*')  # 匹配多行注释
    match = startblockcommentpattern.match(line)
    if match:
        return 'startcommentblock'

    endblockcommentpattern = re.compile(r'[^(\*/)]*\*/[\t ]*')  # 匹配多行注释
    match = endblockcommentpattern.match(line)
    if match:
        return 'endcommentblock'
        
    startpublicclasspattern = re.compile(r' *public *class *\w+ *(implements)* *[\w\. ,]* *(extends)* *[\w\. ,]* *\{')    # 匹配类声明
    match = startpublicclasspattern.match(line)
    if match:
        return 'publicclassblock'

    variablepattern = re.compile(r'[ \t]*[private|public]* *(static)* *(final)* *\w+ +\w+ *=.*')    # 匹配变量声明, 带初始化的
    match = variablepattern.match(line)
    if match:
        return 'variablewithinitvalue'

    variablepattern = re.compile(r'[ \t]*[private|public]* *(static)* *(final)* *\w+ +\w+ *;')    # 匹配变量声明
    match = variablepattern.match(line)
    if match:
        return 'variable'

    functionpattern = re.compile(r'[ \t]*[private|public]* (static)* *(final)* *(synchronized)* *\w+ *\w+\([\w+, ]*\) \w* *\{')    # 匹配函数声明
    match = functionpattern.match(line)
    if match:
        return 'function'

    assignpattern = re.compile(r'[ \t]*[private|public]* *(static)* *(final)* *\w+ *=.*')    # 匹配赋值语句
    match = assignpattern.match(line)
    if match:
        return 'assign'


def dealfunction(line, java):
    print line
    while line:
        line = java.readline()

        if getlinetype(line) == 'linecomment':  # 处理单行注释
            dealcommentline(line, java)
        elif getlinetype(line) == 'startcommentblock':  # 处理注释段
            dealcommentblock(line, java)
        elif getlinetype(line) in ('variable', 'variablewithinitvalue'):  # 处理变量声明
            dealvariable(line, java)
        elif getlinetype(line) == 'function' : # 处理函数定义
            dealfunction(line, java)
        elif getlinetype(line) ==  'assign' : # 处理赋值语句
            dealassignline(line, java)
        # 函数调用
        # 判断语句
        # 循环语句

        if line.endswith('}'):
            break
    print line
        
def dealpackage(line, java):
    jap.package = line
    print line
    
    
def dealpublib(line, java):
    jap.outlib.append(line)
    print line

def dealassignline(line, java):
    print line

def dealcommentline(line, java):
    print line

def dealcommentblock(line, java):
    print line
    while 1:
        line = java.readline()
        print line
        if getlinetype(line) == 'endcommentblock':
            break

def dealpublicclassstart(line, java):
    print line

def dealpublicclass(line, java):
    pubclass = JAClass()
    pubclass.classname = line.split(' ')[1]
    print line
    while line:
        line = java.readline()

        if getlinetype(line) == 'linecomment':  # 处理单行注释
            dealcommentline(line, java)
        elif getlinetype(line) == 'startcommentblock':  # 处理注释段
            dealcommentblock(line, java)
        elif getlinetype(line) in ('variable', 'variablewithinitvalue'):  # 处理成员变量声明
            dealvariable(line, java)
        elif getlinetype(line) == 'function' : # 处理函数定义
            dealfunction(line, java)
        if line.endswith('}'):
            break
    print line

def dealvariable(line, java):
    print line

def dealprivateclass(line, java):
    print line

jap = JAP()


def walk_v1(java):
    line = java.readline()
    while line:
        if getlinetype(line) == 'package': #  处理包
            dealpackage(line, java)
        elif getlinetype(line) == 'publib': #  处理库
            dealpublib(line, java)
        elif getlinetype(line) == 'linecomment':  # 处理单行注释
            dealcommentline(line, java)
        elif getlinetype(line) == 'startcommentblock':  # 处理注释段
            dealcommentblock(line, java)
        elif getlinetype(line) == 'publicclassblock':  # 处理类声明
            dealpublicclass(line, java)
        elif getlinetype(line) == 'privateclassblock':  # 处理类声明
            dealprivateclass(line, java)
        line = java.readline()
    
if __name__ == '__main__':

    # r'[ \t]*[(public)(private)]{0,1} *(static){0,1} *(final){0,1} *[A-Z]\w* *\w+ *=.*'
    # print getlinetype(r'		boolean shouldPlayBeep = true;')


    # variablepattern = re.compile(r'[ \t]*[private|public]* *(static)* *(final)* *\w+ +\w+')    # 匹配变量声明
    # match = variablepattern.match(r'			datasql = "update CM_INVEST_ACCOUNT_INFO set status = "0" where product_id = "+search_value;')
    # if match:
    #     print match.group()
    #     print 'variable'


    # walk(infile)
    walk_v1(infile)